package per.dg.socketclient;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    SocketClienThread socketClienThread;
    private Socket socket;
    private OutputStream outputStream;

    private InputStream inputStream;
    private InputStreamReader inputStreamReader;
    private BufferedReader bufferedReader;

    Button btConnect, btSendMessage;

    EditText etMessage;

    TextView tvResultMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        btConnect = findViewById(R.id.bt_connect);
        btSendMessage = findViewById(R.id.bt_send_message);
        etMessage = findViewById(R.id.et_message);
        tvResultMessage = findViewById(R.id.tv_result_message);
    }

    private Socket connectSocket(String host, int port){
        Socket socket = null;
        try {
            socket = new Socket(host, port);
//            socket.setSoTimeout(30000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return socket;
    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_connect:
                if(socket == null){
//                    new Thread(new Runnable() {
//                        @Override
//                        public void run() {
////                            socket = connectSocket("localhost", 2222);
//                            socket = connectSocket("127.0.0.1", 2222);
//                            socketStateChanged();
////                            receiveServiceMessage();
//                        }
//                    }).start();
                    socketClienThread = new SocketClienThread("127.0.0.1", 2222);
                    socketClienThread.start();
                    socket = socketClienThread.socket;
                }else {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        socket = null;
                    }
                }
                socketStateChanged();
                break;
            case R.id.bt_send_message:
//                if(socket != null){
                    sendMessage();
//                }
                break;
        }
    }

    private void socketStateChanged(){
//        if(socket == null){
//            btConnect.setText("连接 socket");
//            btSendMessage.setEnabled(false);
//        }else {
//            btConnect.setText("关闭 socket");
//            btSendMessage.setEnabled(true);
//        }
    }

    private void sendMessage(){
//        try {
//            if(outputStream == null){
//                outputStream = socket.getOutputStream();
//            }
//            outputStream.write(etMessage.getText().toString().getBytes());
//            outputStream.flush();
//
//            receiveServiceMessage();
//        } catch (IOException e) {
//            e.printStackTrace();
//            Log.e(TAG, "sendMessage: " + e );
//        }
        String s = etMessage.getText().toString();
        if(s != null && !"".equals(s)){
            socketClienThread.addSendMessage(s);
        }else {
            Toast.makeText(this, "发送消息为空", Toast.LENGTH_SHORT).show();
        }

    }

    private void receiveServiceMessage(){
        try {
            if(inputStream == null){
                inputStream = socket.getInputStream();
                inputStreamReader = new InputStreamReader(inputStream);
                bufferedReader = new BufferedReader(inputStreamReader);
            }
            String line;
            while((line = bufferedReader.readLine()) != null) {
                Log.d(TAG, "readWebpageData: " + line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}